package hr.chus.alfresco.struts2.action;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Jan Čustović (jan.custovic@gmail.com)
 */
@SuppressWarnings("serial")
public class FileUploadAction extends ActionSupport {

    private static final Log LOG               = LogFactory.getLog(FileUploadAction.class);

    private List<File>       upload            = new ArrayList<File>();
    private List<String>     uploadFileName    = new ArrayList<String>();
    private List<String>     uploadContentType = new ArrayList<String>();

    private String           status;

    @Override
    public String execute() throws Exception {
        if (!hasErrors()) {
            LOG.info("Received " + upload.size() + " files (" + uploadFileName + ").");
            LOG.info("Content types " + uploadContentType);
            for (File file : upload) {
                LOG.info(file.getName() + "; size: " + file.length());
            }
            status = "Upload done";           
        } else {
            status = "Failed";
        }
        return SUCCESS;
    }

    // Getters & setters

    public void setUpload(List<File> upload) {
        this.upload = upload;
    }

    public void setUploadFileName(List<String> uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public void setUploadContentType(List<String> uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getStatus() {
        return status;
    }
    
    @Override
    public Collection<String> getActionErrors() {
        return super.getActionErrors();
    }

}
